//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BermudanExercise.h"
using namespace Cephei::QL;
#include <gen/QL/EarlyExercise.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CBermudanExercise::CBermudanExercise (Cephei::IVector<DateTime>^ dates, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry) : CEarlyExercise(CBermudanExercise::typeid)
{
    CoVector<DateTime>^ _Cdates;
    try
    {
#ifdef HANDLE
        _phBermudanExercise = NULL;
#endif
        CoVector<DateTime>^ _Cdates = safe_cast<CoVector<DateTime>^> (dates);
        _Cdates->Lock();
        INativeVector<DateTime>^ _NCIdates = _Cdates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdates = safe_cast<CDateTimeVector^>(_NCIdates);
        std::vector<QuantLib::Date>& _dates = static_cast<std::vector<QuantLib::Date>&> (_NCdates->GetReference ());
        bool _payoffAtExpiry = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (payoffAtExpiry) ? (bool)ValueHelper::Convert (payoffAtExpiry->Value) : false); //4
        _ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (new QuantLib::BermudanExercise ( _dates,  _payoffAtExpiry ));
        SetEarlyExercise (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppBermudanExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdates != nullptr) _Cdates->Unlock();
    }
}
Cephei::QL::CBermudanExercise::CBermudanExercise (boost::shared_ptr<QuantLib::BermudanExercise>& childNative, Object^ owner) : CEarlyExercise(CBermudanExercise::typeid)
{
#ifdef HANDLE
	_phBermudanExercise = NULL;
#endif
	_ppBermudanExercise = &childNative;
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppBermudanExercise));
}
Cephei::QL::CBermudanExercise::CBermudanExercise (QuantLib::BermudanExercise& childNative, Object^ owner) : CEarlyExercise(CBermudanExercise::typeid)
{
#ifdef HANDLE
	_phBermudanExercise = NULL;
#endif
	_ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (&childNative);
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppBermudanExercise));
    _BermudanExerciseOwner = owner;
    _EarlyExerciseOwner = owner;
}

Cephei::QL::CBermudanExercise::CBermudanExercise (CBermudanExercise^ copy) : CEarlyExercise(CBermudanExercise::typeid)
{
#ifdef HANDLE
	_phBermudanExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (copy->GetShared());
        _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppBermudanExercise));
    }
}
Cephei::QL::CBermudanExercise::CBermudanExercise (System::Type^ t) : CEarlyExercise(CBermudanExercise::typeid)
{
#ifdef HANDLE
	_phBermudanExercise = NULL;
#endif
	if (!t->IsSubclassOf(CBermudanExercise::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CBermudanExercise::CBermudanExercise (QuantLib::Handle<QuantLib::BermudanExercise>& childNative, Object^ owner)  : CEarlyExercise(CBermudanExercise::typeid)
{
	_phBermudanExercise = &childNative;
	_ppBermudanExercise = &static_cast<boost::shared_ptr<QuantLib::BermudanExercise>>(childNative.currentLink());
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppBermudanExercise));
    _BermudanExerciseOwner = owner;
}
Cephei::QL::CBermudanExercise::CBermudanExercise (QuantLib::Handle<QuantLib::BermudanExercise> childNative)  : CEarlyExercise(CBermudanExercise::typeid)
{
	_phBermudanExercise = &childNative;
	_ppBermudanExercise = &static_cast<boost::shared_ptr<QuantLib::BermudanExercise>>(childNative.currentLink());
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppBermudanExercise));
}
#endif
#ifdef STRUCT
Cephei::QL::CBermudanExercise::CBermudanExercise (QuantLib::BermudanExercise childNative)  : CEarlyExercise(CBermudanExercise::typeid)
{
#ifdef HANDLE
	_phBermudanExercise = NULL;
#endif
	_ppBermudanExercise = new boost::shared_ptr<QuantLib::BermudanExercise> (new QuantLib::BermudanExercise (childNative));
    _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (boost::dynamic_pointer_cast<QuantLib::EarlyExercise> (*_ppBermudanExercise));
}
#endif

Cephei::QL::CBermudanExercise::~CBermudanExercise ()
{
    if (_ppBermudanExercise != NULL)
    {
	    delete _ppBermudanExercise;
        _ppBermudanExercise = NULL;
    }
}
Cephei::QL::CBermudanExercise::!CBermudanExercise ()
{
    if (_ppBermudanExercise != NULL)
    {
	    delete _ppBermudanExercise;
    }
}
QuantLib::BermudanExercise& Cephei::QL::CBermudanExercise::GetReference ()
{
    if (_ppBermudanExercise == NULL) throw gcnew NativeNullException ();
	return **_ppBermudanExercise;
}
boost::shared_ptr<QuantLib::BermudanExercise>& Cephei::QL::CBermudanExercise::GetShared ()
{
    if (_ppBermudanExercise == NULL) throw gcnew NativeNullException ();
	return *_ppBermudanExercise;
}
QuantLib::BermudanExercise* Cephei::QL::CBermudanExercise::GetPointer ()
{
    if (_ppBermudanExercise == NULL) throw gcnew NativeNullException ();
	return &**_ppBermudanExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BermudanExercise>& Cephei::QL::CBermudanExercise::GetHandle ()
{
	if (_phBermudanExercise == NULL)
	{
		_phBermudanExercise = new Handle<QuantLib::BermudanExercise> (*_ppBermudanExercise);
	}
	return *_phBermudanExercise;
}
#endif
bool Cephei::QL::CBermudanExercise::HasNative () 
{
	return (_ppBermudanExercise != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IBermudanExercise^ Cephei::QL::CBermudanExercise_Factory::Create (Cephei::IVector<DateTime>^ dates, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry)
{
    return gcnew CBermudanExercise ( dates,  payoffAtExpiry);
}
